package com.oue.iot.point;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jrain.fw.business.utils.BusinessUtils;
import jrain.fw.core.business.Business;
import jrain.fw.core.business.BusinessConst;
import jrain.fw.core.business.BusinessMethod;
import jrain.fw.core.business.BusinessRequest;
import jrain.fw.core.business.BusinessResponse;
import jrain.fw.core.dao.SqlRunner;
import jrain.fw.core.dao.bean.PageObj;
import jrain.fw.core.utils.FwUtils;
import jrain.fw.dao.common.utils.SqlUtils;
import jrain.utils.collection.CollectionUtils;
import jrain.utils.consts.BaseConst;
import jrain.utils.date.DateUtils;
import jrain.utils.lang.StringUtils;

@Business(name = "IotPointBusiness")
public class IotPointBusiness {
	public SqlRunner getSqlRunner() {
		return FwUtils.getSqlRunnerManger().getSqlRunner(BaseConst.DEFAULT_VALUE);
	}

	@BusinessMethod
	public BusinessResponse add(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		String deviceCode = StringUtils.trimNull(request.getData("deviceCode"));
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		this.setSystemEntCondition(request, paramsMap);
		SqlUtils.addConditionEquals(paramsMap, "deviceCode", deviceCode);
		int countNum = countNum(paramsMap);
		if (countNum == 0) {
			BusinessUtils.setId(request.getData());
			//获取登录用户信息
			String appCode = BusinessUtils.getCurrentAppCode(request);
			String tenantCode = BusinessUtils.getCurrenTenantCode(request);
			request.putData("appCode", appCode);
			request.putData("tenantCode", tenantCode);
			request.putData("createUser", BusinessUtils.getCurrentUserId(request));
			request.putData("createTime", DateUtils.getStrCurrtTime());
			request.putData("lastTime", DateUtils.getStrCurrtTime());
			request.putData("lastUser", BusinessUtils.getCurrentUserId(request));
			int num = getSqlRunner().insert(request.getKey(), "default", request.getData());
			response.setData(num);
		} else {
			response.setCodeAndMsg(BusinessConst.RES_CODE_ERROR_UNKOWN, "设备编码已经存在！");
		}
		return response;
	}

	@BusinessMethod
	public BusinessResponse modify(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		String id = StringUtils.trimNull(request.getData("id"));
		String pointCode = StringUtils.trimNull(request.getData("pointCode"));
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		this.setSystemEntCondition(request, paramsMap);
		SqlUtils.addConditionNotEquals(paramsMap, "id", id);
		SqlUtils.addConditionEquals(paramsMap, "pointCode", pointCode);
		int countNum = countNum(paramsMap);
		if (countNum == 0) {
			request.putData("lastTime", DateUtils.getStrCurrtTime());
			request.putData("lastUser", BusinessUtils.getCurrentUserId(request));
			SqlUtils.addConditionEquals(request.getData(), BusinessConst.DATA_ID,
					request.getData(BusinessConst.DATA_ID));
			this.setSystemEntCondition(request, request.getData());
			int num = getSqlRunner().update(request.getKey(), "default", request.getData());
			response.setData(num);
		} else {
			response.setCodeAndMsg(BusinessConst.RES_CODE_ERROR_UNKOWN, "设备编码已经存在！");
		}
		return response;
	}

	@BusinessMethod
	public BusinessResponse remove(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		List<String> ids = CollectionUtils.valueOfList(StringUtils.trimNull(request.removeData(BusinessConst.DATA_ID)));
		int num = 0;
		if (ids.size() > 0) {
			this.setSystemEntCondition(request, request.getData());
			SqlUtils.addConditionIn(request.getData(), BusinessConst.DATA_ID, ids);
			num = getSqlRunner().delete(request.getKey(), "default", request.getData());
		}
		response.setData(num);
		return response;

	}

	@BusinessMethod
	public BusinessResponse query(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		this.setSystemEntCondition(request, request.getData());
		SqlUtils.addConditionEquals(request.getData(), BusinessConst.DATA_ID, request.getData(BusinessConst.DATA_ID));
		Map<String, Object> bean = getSqlRunner().query(request.getKey(), "default", request.getData());
		response.setData(bean);
		return response;
	}

	@BusinessMethod
	public BusinessResponse count(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		this.setSystemEntCondition(request, request.getData());
		int countNum = countNum(request.getData());
		response.setData(countNum);
		return response;
	}

	@BusinessMethod
	public BusinessResponse list(BusinessRequest request) {
		BusinessResponse response = new BusinessResponse();
		this.setSystemEntCondition(request, request.getData());
		SqlUtils.setSqlOrder(request.getData(), "rowSort asc");
		PageObj<List<Map<String, Object>>> bean = getSqlRunner().listPage("IotPointBusiness.query.v1", "default",
				request.getData());
		response.setData(bean.getRows());
		response.setTotal(bean.getTotal());
		return response;
	}
	
	/**
	 * 设置所属分类
	 * @param request
	 * @return
	 */
	@BusinessMethod
	public BusinessResponse setCatalog(BusinessRequest request){
		BusinessResponse response = new BusinessResponse();
		String catalogId = StringUtils.trimNull(request.getData("catalogId"));
		List<String> ids = CollectionUtils.valueOfList(StringUtils.trimNull(request.removeData("ids")));
		int num = 0;
		if (!StringUtils.isEmpty(catalogId) && ids!=null && ids.size() > 0) {
			Map<String, Object> updateDataMap = new HashMap<String, Object>();
			this.setSystemEntCondition(request, updateDataMap);
			SqlUtils.addConditionIn(updateDataMap, "id", ids);
			updateDataMap.put("catalogId", catalogId);
			num = getSqlRunner().update("IotPointBusiness.modify.v1", "default", updateDataMap);
		}
		response.setData(num);
		return response;
	}

	private int countNum(Map<String, Object> params) {
		Map<String, Object> bean = getSqlRunner().query("IotPointBusiness.count.v1", "default", params);
		Number count = null;
		if (bean != null) {
			count = (Number) bean.get("count");
		}
		return count == null ? 0 : count.intValue();
	}
	
	/**
	 * 设置系统企业约束条件
	 * @param paramMap
	 * @return
	 */
	private Map<String, Object> setSystemEntCondition(BusinessRequest request,Map<String, Object> paramMap){
		//获取登录用户信息
		String appCode = BusinessUtils.getCurrentAppCode(request);
		String tenantCode = BusinessUtils.getCurrenTenantCode(request);
		paramMap.put("appCode", appCode);
		paramMap.put("tenantCode", tenantCode);
		return paramMap;
	}
}
